package com.lottery.ruok.lottrey.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.DrawableRes;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ActivitiesDialogActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.ActivityListResponse;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.utils.DisplayUtil;

import java.util.List;

import butterknife.BindView;
public class ActivitiesItemView extends BaseRelativeLayout {
    //未参加，无状态
    private static final int NO_STARTED = 0;
    //正在进行中
    private static final int PROCESSING = 1;
    //暂停
    private static final int PENDIENT_REVIEW = 2;
    //带领取
    private static final int READY_TO_RECEIVE_THE_REWARDS = 3;
    //已完成
    private static final int COMPLETED = 4;
    @BindView(R.id.activities_image)
    ImageView activitiesImage;
    @BindView(R.id.tv_status)
    TextView tvstatus;
    @BindView(R.id.activities_content)
    TextView activitiesContent;
    @BindView(R.id.progress_bar)
    AlterableProgressBar progressBar;
    @BindView(R.id.wave_view)
    WaveView waveView;
    @BindView(R.id.left_progress)
    AlterableProgressBar leftProgress;
    @BindView(R.id.right_progress)
    AlterableProgressBar rightProgress;
    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.rl)
    LinearLayout rl;
    private ActivityListResponse.ListBean.MissionsBean mData;
    private String mActivitiesId;

    private int itemId;
    private List<ActivityListResponse.ListBean.MissionsBean> missionsBeans;

    public ActivitiesItemView(Context context) {
        super(context);
    }

    public ActivitiesItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ActivitiesItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.activities_item_view);
        activitiesContent.setText("投注金额\n+10,000元...");
        //设置背景动画
        waveView.setDuration(2000);
        waveView.setMaxRadius(DisplayUtil.dip2px(65 / 2, mContext));
        waveView.setStyle(Paint.Style.FILL);
        //设置背景动画的颜色
        waveView.setColor(Color.RED);
        waveView.setInterpolator(new LinearOutSlowInInterpolator());
        //设置任务的状态 已领/未领
//        tvstatus.setVisibility(VISIBLE);
//        tvstatus.getBackground().mutate().setAlpha(200);

        //点击弹窗
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mData != null) {
                    showDialog();
                }
            }
        });
    }

    @Override
    protected void load(Context context) {

    }

    public void setProgressBarVisible() {
        progressBar.setVisibility(VISIBLE);
    }

    private void showDialog() {
        Intent intent = new Intent(mContext, ActivitiesDialogActivity.class);
        intent.putExtra("data", mData);
        intent.putExtra("activities_id", mActivitiesId);
        mContext.startActivity(intent);
    }

    public void setActivitiesImage(@DrawableRes int drawableRes) {
        activitiesImage.setBackgroundResource(drawableRes);
    }

    public void setActivitiesContent(String text) {
        activitiesContent.setText(text);
    }

    public void setData(List<ActivityListResponse.ListBean.MissionsBean> missionsBeans,ActivityListResponse.ListBean.MissionsBean missionsBean, String activitiesId, int activitiesStatus, int index) {
        this.mActivitiesId = activitiesId;
        this.mData = missionsBean;
        this.missionsBeans = missionsBeans;
        Glide.with(mContext).load(missionsBean.getIcon().getImage()).into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                activitiesImage.setBackgroundDrawable(resource);
            }
        });
        String waveViewColor = mData.getIcon().getBorder();
        waveView.setColor(Color.parseColor(waveViewColor.length() == 0 ? "#FF0000" : waveViewColor));
        activitiesContent.setText(missionsBean.getIcon().getDescription());
        activitiesContent.setTextColor(Color.parseColor(missionsBean.getIcon().getColor()));
        progressBar.setProgress(missionsBean.getProgressbar().getValue());
        progressBar.setProgressBarStyle(missionsBean, index);
        switch (missionsBean.getStatus()) {
            case NO_STARTED:
                waveView.setVisibility(GONE);
                break;
            case PROCESSING:
                waveView.setVisibility(VISIBLE);
                waveView.start();
                break;
            case PENDIENT_REVIEW:
                waveView.stop();
                waveView.setVisibility(GONE);
                break;
            case READY_TO_RECEIVE_THE_REWARDS:
                waveView.stop();
                waveView.setVisibility(GONE);
                tvstatus.setVisibility(VISIBLE);
                tvstatus.setBackgroundResource(R.drawable.bg_corners_100_14);
                tvstatus.getBackground().mutate().setAlpha(200);
                tvstatus.setText("未领");
                break;
            case COMPLETED:
                waveView.stop();
                waveView.setVisibility(GONE);
                tvstatus.setVisibility(VISIBLE);
                tvstatus.setBackgroundResource(R.drawable.bg_corners_100_13);
                tvstatus.getBackground().mutate().setAlpha(200);
                tvstatus.setText("已领");
                break;
        }

        if (activitiesStatus == 3 || activitiesStatus == 4 || activitiesStatus == 102) {
            waveView.stop();
            waveView.setVisibility(GONE);
        }
        setProgressBar(missionsBean,index);
    }
    public void setProgressBar(ActivityListResponse.ListBean.MissionsBean missionsBean, int index){
        if (index == missionsBeans.size()-1) {
            rightProgress.setVisibility(INVISIBLE);
        } else {
            rightProgress.setVisibility(VISIBLE);
        }
        if (missionsBean.getProgressbar().getValue() == 100) {
            leftProgress.setProgress(100);
        }
        if (index < missionsBeans.size()-2) {
            ActivityListResponse.ListBean.MissionsBean nextMissionsBean = missionsBeans.get(index + 1);
            if (nextMissionsBean.getProgressbar().getValue() > 0) {
                rightProgress.setProgress(100);
            }
        }
    }
}
